﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTransferBetweenViews.Model;
using System.Collections.ObjectModel;

namespace DataTransferBetweenViews.ViewModel
{
    public class ReceiverViewModel
    {
        private IModel model;
        private ObservableCollection<string> favoriteBooks;

        /// <summary>
        /// Initializes a new instance of the <see cref="ReceiverViewModel"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        public ReceiverViewModel(IModel model)
        {
            // TODO: Complete member initialization
            this.model = model;
            this.model.FavoriteBooksEvent += new EventHandler(this.GetBooks);
        }

        /// <summary>
        /// Gets the favorite books.
        /// </summary>
        /// <value>The favorite books.</value>
        public ObservableCollection<string> FavoriteBooks
        {
            get
            {
                if (this.favoriteBooks == null)
                {
                    this.favoriteBooks = new ObservableCollection<string>();
                }

                return this.favoriteBooks;
            }
        }

        /// <summary>
        /// Gets the books.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GetBooks(object sender, EventArgs e)
        {
            if (sender != null)
            {
                this.FavoriteBooks.Clear();
                foreach (string book in (IList<string>)sender)
                {
                    this.FavoriteBooks.Add(book);
                }
            }
        }
    }
}
